<div class="layui-form" id="selectIPublic" style="display: block;margin: 15px 15px 0;float: left">
    <div class="layui-input-inline">
        <select name="iPublic" id="iPublic" lay-filter="iPublic">
            <option value="0">私有</option>
            <option value="1">公开</option>
        </select>
    </div>
</div>
<div class="layui-form" id="selectResType" style="display: block;margin: 15px 15px 0 0;float: left">
    <div class="layui-input-inline">
        <select name="category" id="category" lay-filter="category">
            <option value="">请选择试题类型</option>
        </select>
    </div>
</div>
<div class="layui-btn layui-btn-sm" style="margin: 15px 0 0">
    <i class="layui-icon">&#xe615;</i>
    <input type="text" name="search" id="search" class="search" placeholder="搜索"/>
</div>
<div class="layui-field-box layui-form">
    <table class="layui-table admin-table">
        <thead>
        <tr>
            <th style="width: 30px;"><input type="checkbox" lay-skin="primary"></th>
            <th>#</th>
            <th>公开/私有</th>
            <th>试题名称</th>
            <th>分类</th>
            <th>标签</th>
        </tr>
        </thead>
        <tbody id="codeContent">
        </tbody>
    </table>
</div>
<div class="admin-table-page" style="border-bottom: none;background-color: transparent;width: auto;bottom: 7px;">
    <div id="codePages" class="page">
    </div>
</div>
<!--模板-->
<script type="text/html" id="codeTpl">
    {{# layui.each(d.value.records, function(index, item){ }}
    <tr>
        <td><input name="codeId" type="checkbox" lay-skin="primary" value="{{item.id}}"></td>
        <td style="max-width: 100px">{{ item.id }}</td>
        {{# if (item.hasPublic == 1) { }}
        <td>公开</td>
        {{# } else { }}
        <td>私有</td>
        {{# } }}
        <td>{{ item.name }}</td>
        <td style="word-wrap:break-word;max-width: 150px;">{{ item.categoryName}}</td>
        <td style="word-wrap:break-word;max-width: 150px;">{{ item.tagName}}</td>
    </tr>
    {{# });
    }}
</script>
<script src="../js/jquery-1.10.0.js"></script>
<script src="../js/template-native.js"></script>
<script>
    layui.use(['paging', 'form', 'busajax'], function () {
        var $ = layui.jquery,
                bajax = layui.busajax,
                paging = layui.paging(),
                layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
                layer = layui.layer, //获取当前窗口的layer对象
                form = layui.form();
        var resdatas = {
            page: {
                current: "1",
                size: 7
            },
            keyword : "",
            publicType : $("#iPublic").val() == 1 ? 1 : 0,
            categoryId : 0
        };
        paging.init({
            openWait: true,
            url: HOST+'business.ojProblem.getProblemByPage.hsr', //地址
            elem: '#codeContent', //内容容器
            params: resdatas,
            type: 'POST',
            tempElem: '#codeTpl', //模块容器
            pageConfig: { //分页参数配置
                elem: '#codePages', //分页容器
                pageSize: 7 //分页大小
            },
            success: function () { //渲染成功的回调
//                alert('渲染成功');
            },
            fail: function (msg) { //获取数据失败的回调
                alert('获取数据失败')
            },
            complate: function () { //完成的回调
//                alert('处理完成');
                //重新渲染复选框
                form.render();
                form.on('checkbox(allselector)', function (data) {
                    var elem = data.elem;

                    $('#content').children('tr').each(function () {
                        var $that = $(this);
                        //全选或反选
                        $that.children('td').eq(0).children('input[type=checkbox]')[0].checked = elem.checked;
                        form.render('checkbox');
                    });
                });
            }
        });

        $.ajax({
            url: HOST + 'business.ojProblemCategory.getAllProblemCategoryList.hsr',
            type: 'POST',
            headers: {
                token: JSON.parse(getCookie('user')).token
            },
            data: {},
            dataType: "json",
            success: function(result){
                if (result.code == 0 && result.value) {
                    $('#category').append("<option value='0'>所有类型</option>");
                    for (var i= 0;i < result.value.length;i++) {
                        $('#category').append("<option value='"+result.value[i].id+"'>"+result.value[i].categoryName+"</option>");
                    }
                    form.render();
                }
            },
            error:function(xhr){
                alert("错误提示： " + xhr.status + " " + xhr.statusText);
            }
        });

        $('#search').keyup(function (e) {
            if(e.keyCode == 13){
                resdatas={
                    page:{
                        current:"1",
                        size: 7
                    },
                    user:{
                        id: getCookie('user') === "" ? "" : JSON.parse(getCookie('user')).id
                    },
                    keyword : $(this).val(),
                    categoryId : $("#category").val()?$("#category").val():0,
                    publicType : $("#iPublic").val() == 1 ? 1 : 0
                };
                paging.get(JSON.stringify(resdatas));
            }
        });

        form.on('select(category)', function(type){
            resdatas={
                page:{
                    current : "1",
                    size: 7
                },
                user:{
                    id: getCookie('user') === "" ? "" : JSON.parse(getCookie('user')).id
                },
                keyword : $("#search").val(),
                categoryId : type.value?type.value:0,
                publicType : $("#iPublic").val() == 1 ? 1 : 0
            };
            paging.get(JSON.stringify(resdatas));
        });

        form.on('select(iPublic)', function(type){
            resdatas={
                page:{
                    current : "1",
                    size: 7
                },
                user:{
                    id: getCookie('user') === "" ? "" : JSON.parse(getCookie('user')).id
                },
                keyword : $("#search").val(),
                categoryId : $("#category").val()?$("#category").val():0,
                publicType : type.value == 1 ? 1 : 0
            };
            paging.get(JSON.stringify(resdatas));
        });
    });
</script>
